﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ControllerSimulator {
    public partial class Form1 : Form {
        private SerialPort m_comport;
        public Form1() {
            InitializeComponent();
            m_comport = new SerialPort();
        }

        private void button1_Click(object sender, EventArgs e) {
            try {
                StartPort();
                int controller = int.Parse(m_txtControllerId.Text);
                int bicycleId = int.Parse(m_txtBicycleId.Text);
                SendSelectItem(controller, bicycleId, true);     
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }

           

        }
        private void StartPort() {
            if (m_comport.IsOpen) {
                m_comport.Close();
            }
            m_comport.PortName = m_txtPortname.Text;
            m_comport.Encoding = Encoding.ASCII;
            m_comport.BaudRate = 9600;
            m_comport.DataBits = 8;
            m_comport.StopBits = StopBits.Two;
            m_comport.Parity = Parity.None;
            m_comport.ReadBufferSize = 4096;
            m_comport.NewLine = "\r\n";
            m_comport.Handshake = Handshake.None;
            m_comport.ReceivedBytesThreshold = 100000;
            m_comport.Open();
        }
        private void SendSelectItem(int controllerId, int bikeId, bool lockBicyle) {
            try {
                //write line to serial port
                byte[] data = new byte[17];
                long now = DateTime.Now.Ticks;
                data[0] = (byte)(bikeId >> 24);
                data[1] = (byte)(bikeId >> 16);
                data[2] = (byte)(bikeId >> 8);
                data[3] = (byte)(bikeId);
                var time = BitConverter.GetBytes(now);
                data[4] = time[0];
                data[5] = time[1];
                data[6] = time[2];
                data[7] = time[3];
                data[8] = time[4];
                data[9] = time[5];
                data[10] = time[6];
                data[11] = time[7];

                data[12] = (byte)(controllerId >> 24);
                data[13] = (byte)(controllerId >> 16);
                data[14] = (byte)(controllerId >> 8);
                data[15] = (byte)(controllerId);

                data[16] = Convert.ToByte(lockBicyle);
                m_comport.Write(data, 0, 17);
            } catch (System.Exception ex) {
                //baudRatelLabel.Text = ex.Message;
            }
        }

        private void button2_Click(object sender, EventArgs e) {
            StartPort();
            int controller = int.Parse(m_txtControllerId.Text);
            int bicycleId = int.Parse(m_txtBicycleId.Text);
            SendSelectItem(controller, bicycleId, false);
        }

        private void button3_Click(object sender, EventArgs e) {
            string[] ports = SerialPort.GetPortNames();
            MessageBox.Show(string.Join(";",ports));
        }
    }
}
